/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.helper.editing_support;

import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;

import de.cbf.cam.dsa.facet.talent.edit.ControllerEditBeSources;
import de.cbf.cam.dsa.facet.talent.helper.BeTypeValue;
import de.cbf.cam.dsa.facet.talent.test_support.ConstantsSWTBOTTalent;
import de.cbf.util.ui.helper.ICustomizableEditingSupport;

/**
 * @author salter
 *
 */
public class EditingSupport_BeSources_MathType  extends EditingSupport implements ICustomizableEditingSupport{
	private static final Log logger = LogFactory.getLog(EditingSupport_BeSources_MathType.class);
	
	protected TableViewer tableViewer;
	
	protected Map<String, Integer> operationsMapping = new HashMap<String, Integer>();
	
	public EditingSupport_BeSources_MathType(ColumnViewer viewer) {
		super(viewer);
		if (viewer instanceof TableViewer) {
			TableViewer tableViewer = (TableViewer)viewer;
			this.tableViewer = tableViewer;
		}else{
			String err_msg = "entity of wrong type in EditingSupport_BeSources_MathType : \n"+
					"Expected: "+TableViewer.class.getSimpleName()+"\n"+ 
					"Got: "+ (viewer == null ? "null" : viewer.getClass().getSimpleName());
			logger.error(err_msg);
		}
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		Set<String> keys = operationsMapping.keySet();
		String[] items = keys.toArray(new String[keys.size()]);
		Arrays.sort(items); //comes from map ->needs sorting
		ComboBoxViewerCellEditor result = new ComboBoxViewerCellEditor(tableViewer.getTable(),SWT.CENTER|SWT.READ_ONLY){
			//need to overwrite, or minimum width could be too big
			public LayoutData getLayoutData() {
				LayoutData layoutData = super.getLayoutData();
				layoutData.minimumWidth = ControllerEditBeSources.WIDTH_COLUMN_MATH_TYPE;
				return layoutData;
			}
		};		
		result.getControl().setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_BE_SOURCES_TABLE_COMBO_MATH_TYPE_ID);
		result.setContentProvider(new ArrayContentProvider());
		result.setInput(items);		
		result.setLabelProvider(new LabelProvider(){
			@Override
			public String getText(Object element) {
				if (element instanceof String) {
					String text = (String) element;					
					return text;
				}
				return "unDef";
			}			
		});
		return result;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
	 */
	@Override
	protected Object getValue(Object element) {
		if (element instanceof BeTypeValue) {
			BeTypeValue beSource = (BeTypeValue)element;
			Integer operationConstant = beSource.getMathType();
			String text = getTextForOperationType(operationConstant);
			return text;
		}else{
			String err_msg = "entity of wrong type in getValue : \n"+
					"Expected: "+BeTypeValue.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
	 */
	@Override
	protected void setValue(Object element, Object value) {
		if (element instanceof BeTypeValue && value instanceof String) {
			BeTypeValue beSource = (BeTypeValue)element;
			String valueString = (String)value;
			Integer operationConstant = operationsMapping.get(valueString);
			beSource.setMathType(operationConstant);
			tableViewer.update(element, null);	
		}else{
			String err_msg = "entity of wrong type in setValue : \n"+
					"Expected: "+BeTypeValue.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		};
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.helper.ICustomizableEditingSupport#customize(java.lang.Object)
	 */
	@Override
	public void customize(Object obj) {
		// expects with map of operationsmapping
		if (obj instanceof Map) {
			Map<?,?> map = (Map<?,?>) obj;
			Set<?> keys = map.keySet();
			for (Object key : keys) {
				Object value = map.get(key);
				if (key instanceof String && value instanceof Integer) {
					String operationSymbol = (String) key;
					Integer operationConstant = (Integer) value;
					operationsMapping.put(operationSymbol, operationConstant);
				} 
			}
		} else {
			String err_msg = "entity of wrong type in customize : \n"
					+ "Expected: " + Map.class.getSimpleName() + "\n"
					+ "Got: "+ (obj == null ? "null" : obj.getClass().getSimpleName());
			logger.error(err_msg);
		}
	}

	protected String getTextForOperationType(Integer operationConstant){
		for (Entry<String, Integer> entry:operationsMapping.entrySet()) {
			if(operationConstant.equals(entry.getValue())){
				return entry.getKey();
			}
		}
		return "ukn";
	}
}
